//
//  ViewController.m
//  综合练习goods
//
//  Created by 小白 on 16/2/28.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController.h"
#import "goods.h"

@interface ViewController ()

@property (nonatomic,copy) NSArray *goods;
//@property (nonatomic,weak) UILabel *alreadyCount;
//@property (nonatomic,weak) UILabel *totalCount;
@end

@implementation ViewController
-(NSArray *)goods
{
    if (_goods == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"goods" ofType:@"plist"];
        NSArray *arr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *element in arr)
        {
            goods *good = [goods goodsWithDictionary:element];
            [mArr addObject:good];
        }
        _goods = mArr;
    }
    return _goods;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    for (int i = 0; i < self.goods.count; i++) {
        
    
        [self add];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)add{
    int index = self.goodsView.subviews.count;
    int columecount = 2;
    int row = index/columecount;
    int colume = index%columecount;
    int goodsViewWidth = self.goodsView.frame.size.width;
    int imageW = 130;
    int imageH = 130;
    int imageMargin = (goodsViewWidth - columecount*imageW)/(columecount-1);
    int rowMargin = 30;
    int col = colume*(imageW+imageMargin);
    int rowP = row*(imageH + rowMargin);
    goods *goods = self.goods[index];
    UIView *sbview = [[UIView alloc]initWithFrame:CGRectMake(col, rowP, imageW, imageH+30)];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageW, imageH)];
    img.image = [UIImage imageNamed:goods.picture];
    [sbview addSubview:img];
    UILabel *alreadyCount = [[UILabel alloc]initWithFrame:CGRectMake(0, imageH, 100, 10)];
    alreadyCount.text = [NSString stringWithFormat:@"总需参与%@",goods.alreadyCount];
    alreadyCount.textAlignment = NSTextAlignmentCenter;
    alreadyCount.font = [UIFont systemFontOfSize:10];
    [sbview addSubview:alreadyCount];
    
    UILabel *totalCount = [[UILabel alloc]initWithFrame:CGRectMake(0, imageH+20, 100, 10)];
    totalCount.textAlignment = NSTextAlignmentCenter;
    totalCount.text = [NSString stringWithFormat:@"参与%@",goods.totalCount];
    totalCount.font = [UIFont systemFontOfSize:10];
    totalCount.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    
    [sbview addSubview:totalCount];
    UIImageView *imgLogo = [[UIImageView alloc]initWithFrame:CGRectMake(100, imageH, 30, 30)];
    imgLogo.image = [UIImage imageNamed:@"tfw_gwc"];
    [sbview addSubview:imgLogo];
    [self.goodsView addSubview:sbview];
    
}










@end
